package com.yuntongxun.ecdemo.voicechat.util;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.TextView;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.storage.ContactSqlManager;
import com.yuntongxun.ecdemo.ui.contact.ECContacts;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.view.MyImageSpan;

import java.io.IOException;
import java.io.InputStream;

/**
 * 获取本地个人信息
 */

public class InfoUtil {

    public static UserInfo getLocalUserInfo(Context context){
        String data=PreferenceUtils.getPrefString(context, Contants.Preference.user_info,"");
        if (!data.equals("")){
            JsonHelper<UserInfo> jsonHelper = new JsonHelper<UserInfo>(UserInfo.class);
            return jsonHelper.getData(data, "result");
        }
        return null;
    }

    public static String getNickName(String info){
        if (info==null){
            return "";
        }
        if (info.contains("({")) {
            return translate(info);
        }else{


            ECContacts ecContacts= ContactSqlManager.getContact(info);
            if (ecContacts!=null){
                    return translate(ecContacts.getNickname());
            }
        }
        return info;
    }

    private static String translate(String info){
        int index = info.indexOf("(");
        String message = info.substring(index + 1, info.length() - 1);
        UserInfo infos = null;
        try {
            infos = parseResult(message);
            if (infos!=null){
                return infos.getNickName();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    public static UserInfo parseResult(String result) {
        JsonHelper<UserInfo> dataParser = new JsonHelper<UserInfo>(
                UserInfo.class);
        return dataParser.getData(
                result, "result");
    }

    public static void setNickName(UserInfo infos, Context mContext, TextView infoTv){
        MyImageSpan flagSpan=null;
        MyImageSpan genderSpan;
        if (infos!=null) {
            String name = infos.getNickName() + "  ";
            String age = "  " + infos.getAge() + "  ";
            String nativel = " ";
            String learn = " ";
            if (infos.getNativeLanguage() != null) {
                nativel = "  " + infos.getNativeLanguage() + "  ";
            }
            if (infos.getLearningLanguage() != null) {
                learn = "  " + infos.getLearningLanguage() + "";
            }
//
//
            if (infos.getGender()!=null&&infos.getGender().equals("男")) {
                genderSpan = new MyImageSpan(mContext, R.drawable.ic_man);
            } else {
                genderSpan = new MyImageSpan(mContext, R.drawable.ic_woman);
            }

            MyImageSpan nativeSpan = new MyImageSpan(mContext, R.drawable.ic_speak);
            MyImageSpan learnSpan = new MyImageSpan(mContext, R.drawable.ic_learn);

            try {
                InputStream is = mContext.getAssets().open("flag/" + infos.getCountry() + ".png");
                flagSpan = new MyImageSpan(mContext, BitmapFactory.decodeStream(is));
                is.close();
            } catch (IOException e) {
                Log.e("----", e.toString());
            }

            infoTv.setText("");
            //姓名
            SpannableString nameShow = new SpannableString(name);
            ForegroundColorSpan blackSpan = new ForegroundColorSpan(Color.BLACK);
            nameShow.setSpan(blackSpan, 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            infoTv.append(nameShow);

            //插入性别图标
            SpannableString showString = new SpannableString("1");
            showString.setSpan(genderSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            infoTv.append(showString);

            //年龄颜色
            SpannableString ageShow = new SpannableString(age);
            ForegroundColorSpan yellowSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.voice_yellow));
            ageShow.setSpan(yellowSpan, 0, age.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            infoTv.append(ageShow);

            if (flagSpan!=null){
                //插入国旗图标
                showString.setSpan(flagSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                infoTv.append(showString);
            }


            infoTv.append("  ");

            //插入本地语言的图标
            showString.setSpan(nativeSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            infoTv.append(showString);

            //本地语言颜色
            SpannableString nativeShow = new SpannableString(nativel);
            ForegroundColorSpan greenSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.voice_green));
            nativeShow.setSpan(greenSpan, 0, nativel.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            infoTv.append(nativeShow);

            //插入学习的图标
            showString.setSpan(learnSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            infoTv.append(showString);

            //学习语言颜色
            SpannableString learnShow = new SpannableString(learn);
            ForegroundColorSpan greenSpan1 = new ForegroundColorSpan(mContext.getResources().getColor(R.color.voice_green));
            learnShow.setSpan(greenSpan1, 0, learn.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            infoTv.append(learnShow);
        }
    }
}
